﻿using MySql.Data.MySqlClient;
using PelitaProduksi.Controller;
using PelitaProduksi.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PelitaProduksi.View.Panel
{
    public partial class GroupPegawai : Form
    {
        Pegawai p1, p2, p3;
        ComboBox text1, text2, text3;
        public GroupPegawai(ComboBox text1, ComboBox text2, ComboBox text3, Pegawai p1, Pegawai p2, Pegawai p3)
        {
            InitializeComponent();
            this.text1 = text1;
            this.text2 = text2;
            this.text3 = text3;
            this.p1 = p1;
            this.p2 = p2;
            this.p3 = p3;
            initData();
        }

        private void initData()
        {
            list.Items.Add(""); // NONE
            List<string> temp = Pegawai.listAllKelompok();
            foreach (string s in temp)
            {
                list.Items.Add(s);
            }
        }

        private void list_SelectedIndexChanged(object sender, EventArgs e)
        {
            pegawai1.Text = "";
            pegawai2.Text = "";
            pegawai3.Text = "";
            if (list.SelectedItems != null && list.SelectedItems.Count>0)
            {
                string s = list.SelectedItems[0].ToString();
                if (s.Length > 0)
                {
                    List<Pegawai> temp = Pegawai.listAllPegawaiByKelompok(s);
                    int i = temp.Count;
                    if (i > 0)
                    {
                        p1 = temp[0];
                        pegawai1.Text = p1.Nama;
                    }
                    if (i > 1)
                    {
                        p2 = temp[1];
                        pegawai2.Text = p2.Nama;
                    }
                    if (i > 2)
                    {
                        p3 = temp[2];
                        pegawai3.Text = p3.Nama;
                    }
                }
                else
                {
                    pegawai1.Text = "";
                    pegawai2.Text = "";
                    pegawai3.Text = "";
                }
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            //text1.Text = pegawai1.Text;
            //text2.Text = pegawai2.Text;
            //text3.Text = pegawai3.Text;
            int i1, i2, i3;
            i1=i2=i3=-1;
            for(int i=0; i<text1.Items.Count; i++)
            {
                object o = text1.Items[i];
                if(o.ToString().Equals(pegawai1.Text))
                {
                    i1 = i;
                    break;
                }
            }
            for (int i = 0; i < text2.Items.Count; i++)
            {
                object o = text2.Items[i];
                if (o.ToString().Equals(pegawai2.Text))
                {
                    i2 = i;
                    break;
                }
            }
            for (int i = 0; i < text3.Items.Count; i++)
            {
                object o = text3.Items[i];
                if (o.ToString().Equals(pegawai3.Text))
                {
                    i3 = i;
                    break;
                }
            }
            if(i1!=-1)
            text1.SelectedIndex = i1;
            if (i2 != -1)
            text2.SelectedIndex = i2;
            if (i3 != -1)
            text3.SelectedIndex = i3;
            this.Close();
        }
    }
}
